home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / actassst / mdinote.ba_ / mdinote.ba
Encoding:
Text File  |  1998-02-25  |  8.9 KB  |  259 lines

  1. Attribute VB_Name = "Module1"
  2. '*** Global module for MDI Notepad sample application.  ***
  3. '**********************************************************
  4. Option Explicit
  5.  
  6. ' User-defined type to store information about child forms
  7. Type FormState
  8.     Deleted As Integer
  9.     Dirty As Integer
  10.     Color As Long
  11. End Type
  12.  
  13. Public FState()  As FormState           ' Array of user-defined types
  14. Public Document() As New frmNotePad     ' Array of child form objects
  15. Public gFindString As String            ' Holds the search text.
  16. Public gFindCase As Integer             ' Key for case sensitive search
  17. Public gFindDirection As Integer        ' Key for search direction.
  18. Public gCurPos As Integer               ' Holds the cursor location.
  19. Public gFirstTime As Integer            ' Key for start position.
  20. Public gToolsHidden As Boolean          ' Holds toolbar state.
  21. Public Const ThisApp = "MDINote"        ' Registry App constant.
  22. Public Const ThisKey = "Recent Files"   ' Registry Key constant.
  23. Public strSearch As String              ' Holds the return string from the assistant.
  24.  
  25.  
  26. Function AnyPadsLeft() As Integer
  27.     Dim i As Integer        ' Counter variable
  28.  
  29.     ' Cycle through the document array.
  30.     ' Return true if there is at least one open document.
  31.     For i = 1 To UBound(Document)
  32.         If Not FState(i).Deleted Then
  33.             AnyPadsLeft = True
  34.             Exit Function
  35.         End If
  36.     Next
  37. End Function
  38.  
  39.  
  40. Sub EditCopyProc()
  41.     ' Copy the selected text onto the Clipboard.
  42.     Clipboard.SetText frmMDI.ActiveForm.ActiveControl.SelText
  43. End Sub
  44.  
  45. Sub EditCutProc()
  46.     ' Copy the selected text onto the Clipboard.
  47.     Clipboard.SetText frmMDI.ActiveForm.ActiveControl.SelText
  48.     ' Delete the selected text.
  49.     frmMDI.ActiveForm.ActiveControl.SelText = ""
  50. End Sub
  51.  
  52. Sub EditPasteProc()
  53.     ' Place the text from the Clipboard into the active control.
  54.     frmMDI.ActiveForm.ActiveControl.SelText = Clipboard.GetText()
  55. End Sub
  56. Sub Assistant()
  57.     Dim MyAssistant As Assistant
  58.     
  59.     Set MyAssistant = frmMDI.Assistant1
  60.     ' Display Assistant if not already visible.
  61.     If MyAssistant.AssistantVisible = False Then
  62.         MyAssistant.Reduced = True
  63.         ''MyAssistant.Visible = True
  64.         MyAssistant.AssistantShow
  65.     End If
  66.  
  67. '    ' Play Assistant Animantion at random intervals if Assistant is visible
  68. '    Interval = Int((5 * Rnd) + 1)    ' Generate random value between 1 and 5 Mins.
  69. '    Interval = Interval * 100
  70.     
  71. '    ' Set Timer Interval
  72. '    frmMDI.Timer1.Interval = Interval
  73. '    frmMDI.Timer1.Enabled = Not frmMDI.Timer1.Enabled
  74. '    If Not frmMDI.Timer1.Enabled Then MyAssistant.StopAnimation
  75.  
  76.     Set MyAssistant = Nothing
  77.     ' Set focus back to the toolbar form.
  78.     frmMDI.SetFocus
  79. End Sub
  80. Sub FileNew()
  81.     Dim fIndex As Integer
  82.  
  83.     ' Find the next available index and show the child form.
  84.     fIndex = FindFreeIndex()
  85.     Document(fIndex).Tag = fIndex
  86.     Document(fIndex).Caption = "Untitled:" & fIndex
  87.     Document(fIndex).Show
  88.  
  89.     ' Make sure the toolbar edit buttons are visible.
  90.     frmMDI.imgCutButton.Visible = True
  91.     frmMDI.imgCopyButton.Visible = True
  92.     frmMDI.imgPasteButton.Visible = True
  93. End Sub
  94.  
  95. Function FindFreeIndex() As Integer
  96.     Dim i As Integer
  97.     Dim ArrayCount As Integer
  98.  
  99.     ArrayCount = UBound(Document)
  100.  
  101.     ' Cycle through the document array. If one of the
  102.     ' documents has been deleted, then return that index.
  103.     For i = 1 To ArrayCount
  104.         If FState(i).Deleted Then
  105.             FindFreeIndex = i
  106.             FState(i).Deleted = False
  107.             Exit Function
  108.         End If
  109.     Next
  110.  
  111.     ' If none of the elements in the document array have
  112.     ' been deleted, then increment the document and the
  113.     ' state arrays by one and return the index to the
  114.     ' new element.
  115.     ReDim Preserve Document(ArrayCount + 1)
  116.     ReDim Preserve FState(ArrayCount + 1)
  117.     FindFreeIndex = UBound(Document)
  118. End Function
  119.  
  120. Sub FindIt()
  121.     Dim intStart As Integer
  122.     Dim intPos As Integer
  123.     Dim strFindString As String
  124.     Dim strSourceString As String
  125.     Dim strMsg As String
  126.     Dim intResponse As Integer
  127.     Dim intOffset As Integer
  128.     
  129.     ' Set offset variable based on cursor position.
  130.     If (gCurPos = frmMDI.ActiveForm.ActiveControl.SelStart) Then
  131.         intOffset = 1
  132.     Else
  133.         intOffset = 0
  134.     End If
  135.  
  136.     ' Read the public variable for start position.
  137.     If gFirstTime Then intOffset = 0
  138.     ' Assign a value to the start value.
  139.     intStart = frmMDI.ActiveForm.ActiveControl.SelStart + intOffset
  140.         
  141.     ' If not case sensitive, convert the string to upper case
  142.     If gFindCase Then
  143.         strFindString = gFindString
  144.         strSourceString = frmMDI.ActiveForm.ActiveControl.Text
  145.     Else
  146.         strFindString = UCase(gFindString)
  147.         strSourceString = UCase(frmMDI.ActiveForm.ActiveControl.Text)
  148.     End If
  149.             
  150.     ' Search for the string.
  151.     If gFindDirection = 1 Then
  152.         intPos = InStr(intStart + 1, strSourceString, strFindString)
  153.     Else
  154.         For intPos = intStart - 1 To 0 Step -1
  155.             If intPos = 0 Then Exit For
  156.             If Mid(strSourceString, intPos, Len(strFindString)) = strFindString Then Exit For
  157.         Next
  158.     End If
  159.  
  160.     ' If the string is found...
  161.     If intPos Then
  162.         frmMDI.ActiveForm.ActiveControl.SelStart = intPos - 1
  163.         frmMDI.ActiveForm.ActiveControl.SelLength = Len(strFindString)
  164.     Else
  165.         strMsg = "Cannot find " & Chr(34) & gFindString & Chr(34)
  166.         intResponse = MsgBox(strMsg, 0, App.Title)
  167.     End If
  168.     
  169.     ' Reset the public variables
  170.     gCurPos = frmMDI.ActiveForm.ActiveControl.SelStart
  171.     gFirstTime = False
  172. End Sub
  173.  
  174. Sub GetRecentFiles()
  175.     ' This procedure demonstrates the use of the GetAllSettings function,
  176.     ' which returns an array of values from the Windows registry. In this
  177.     ' case, the registry contains the files most recently opened.  Use the
  178.     ' SaveSetting statement to write the names of the most recent files.
  179.     ' That statement is used in the WriteRecentFiles procedure.
  180.     Dim i, j As Integer
  181.     Dim varFiles As Variant ' Varible to store the returned array.
  182.     
  183.     ' Get recent files from the registry using the GetAllSettings statement.
  184.     ' ThisApp and ThisKey are constants defined in this module.
  185.     If GetSetting(ThisApp, ThisKey, "RecentFile1") = Empty Then Exit Sub
  186.     
  187.     varFiles = GetAllSettings(ThisApp, ThisKey)
  188.     
  189.     For i = 0 To UBound(varFiles, 1)
  190.         
  191.         frmMDI.mnuRecentFile(0).Visible = True
  192.         frmMDI.mnuRecentFile(i).Caption = varFiles(i, 1)
  193.         frmMDI.mnuRecentFile(i).Visible = True
  194.             ' Iterate through all the documents and update each menu.
  195.             For j = 1 To UBound(Document)
  196.                 If Not FState(j).Deleted Then
  197.                     Document(j).mnuRecentFile(0).Visible = True
  198.                     Document(j).mnuRecentFile(i + 1).Caption = varFiles(i, 1)
  199.                     Document(j).mnuRecentFile(i + 1).Visible = True
  200.                 End If
  201.             Next j
  202.     Next i
  203.  
  204. End Sub
  205.  
  206. Sub OptionsToolbarProc(CurrentForm As Form)
  207.     
  208.     ' Toggle the check
  209.         CurrentForm.mnuOptionsToolbar.Checked = Not CurrentForm.mnuOptionsToolbar.Checked
  210.     ' If not the MDI form, set the MDI form's check.
  211.     If Not TypeOf CurrentForm Is MDIForm Then
  212.         frmMDI.mnuOptionsToolbar.Checked = CurrentForm.mnuOptionsToolbar.Checked
  213.     End If
  214.     gToolsHidden = frmMDI.mnuOptionsToolbar.Checked
  215.     ' Toggle the toolbar based on the value.
  216.     If gToolsHidden Then
  217.         frmMDI.picToolbar.Visible = True
  218.     Else
  219.         frmMDI.picToolbar.Visible = False
  220.     End If
  221. End Sub
  222.  
  223. Sub WriteRecentFiles(OpenFileName)
  224.     ' This procedure uses the SaveSettings statement to write the names of
  225.     ' recently opened files to the System registry. The SaveSetting
  226.     ' statement requires three parameters. Two of the parameters are
  227.     ' stored as constants and are defined in this module.  The GetAllSettings
  228.     ' function is used in the GetRecentFiles procedure to retrieve the
  229.     ' file names stored in this procedure.
  230.     
  231.     Dim i, j As Integer
  232.     Dim strFile, Key As String
  233.  
  234.     ' Copy RecentFile1 to RecentFile2, and so on.
  235.     For i = 3 To 1 Step -1
  236.         Key = "RecentFile" & i
  237.         strFile = GetSetting(ThisApp, ThisKey, Key)
  238.         If strFile <> "" Then
  239.             Key = "RecentFile" & (i + 1)
  240.             SaveSetting ThisApp, ThisKey, Key, strFile
  241.         End If
  242.     Next i
  243.   
  244.     ' Write the open file to first recent file.
  245.     SaveSetting ThisApp, ThisKey, "RecentFile1", OpenFileName
  246. End Sub
  247.  
  248.  
  249. Public Sub AnimateAssis()
  250.     Dim frmcount As Integer
  251.     
  252.     frmcount = frmMDI.Assistant1.FrameCount
  253.     ' Play animation.
  254.     If frmMDI.Assistant1.AssistantVisible And Not frmMDI.Assistant1.IsAnimationPlaying Then
  255.         frmMDI.Assistant1.PlayAnimation 1, frmcount, False
  256.     End If
  257.  
  258. End Sub
  259.